using System;
using System.Collections.Generic;
using System.Text;
using Wicresoft.OmsClient.OmsService;

namespace Wicresoft.OmsClient
{
    class UserService
    {
        private static bool flag;

        public static bool IsAuthorized
        {
            get { return UserService.flag; }
            set { UserService.flag = value; }
        }

        private static OMSService oms;

        #region old
        public static string GetEmail()
        {
            //string email = SyncConfigManage.Email;
            //if (email != "")
            //{
            //    return email;
            //}
            if (oms == null)
            {
                oms = new OMSService();                
            }
            oms.Url = SyncConfigManage.ProviderUrl;
            if (oms.Url == "")
                return "";
            return oms.GetEmailByCSL(Environment.UserName);          
            
        }

        public static string GetMoblie()
        {
            if (oms == null)
            {
                oms = new OMSService();
            }
            oms.Url = SyncConfigManage.ProviderUrl;
            if (oms.Url == "")
                return "";
            return oms.GetMobileByCSL(Environment.UserName);    
        }

        public static void GetInfoFromOMS()
        {
            string email = SyncConfigManage.Email;
            if (email == "")
            {
                SyncConfigManage.Email = GetEmail();
            }
            string mobile = SyncConfigManage.Mobile;
            if (mobile == "")
            {
                SyncConfigManage.Mobile = GetMoblie();
            }
        }

        public static bool CheckEmail(string email)
        {
            if (oms == null)
            {
                oms = new OMSService();                
            }
            oms.Url = SyncConfigManage.ProviderUrl;
            if (oms.Url == "")
                return false;
            return oms.CheckEmail(email);
        }

        #endregion

        /// <summary>
        /// -2 exception -1 no connection 0 false 1 true
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static int CheckUser(string username, string pwd)
        {
            try
            {
                if (oms == null)
                {
                    oms = new OMSService();
                }
                oms.Url = SyncConfigManage.ProviderUrl;
                if (oms.Url == "")
                    return -1;
                if (oms.AuthUser(username, pwd))
                    return 1;
                else
                    return 0;
            }
            catch (Exception ex)
            {
                Logger.Exception("UserService.CheckUser ", ex);
                return -2;
            }
            
        }

        public static void GetUserInfoFromOMS()
        {
            try
            {
                if (oms == null)
                {
                    oms = new OMSService();
                }
                oms.Url = SyncConfigManage.ProviderUrl;
                if (oms.Url == "")
                    return;

                OmsService.UserInfo ui = oms.GetUserInfoByCSL(Environment.UserName);
                if (ui != null)
                {
                    if (string.IsNullOrEmpty(SyncConfigManage.UserName))
                        SyncConfigManage.UserName = ui.CIL;
                    if (string.IsNullOrEmpty(SyncConfigManage.Email))
                        SyncConfigManage.Email = ui.Email;
                    if (string.IsNullOrEmpty(SyncConfigManage.Mobile))
                        SyncConfigManage.Mobile = ui.Mobile;
                }
            }
            catch (Exception ex)
            {
                Logger.Exception("UserService.GetUserInfoFromOMS ", ex);
            }           
        }

        public static void GetUserInfoFromOMS(string cil)
        {
            try
            {
                if (oms == null)
                {
                    oms = new OMSService();
                }
                oms.Url = SyncConfigManage.ProviderUrl;
                if (oms.Url == "")
                    return;

                OmsService.UserInfo ui = oms.GetUserInfoByCIL(cil);
                if (ui != null)
                {
                    SyncConfigManage.UserName = ui.CIL;
                    SyncConfigManage.Email = ui.Email;
                }
            }
            catch (Exception ex)
            {
                Logger.Exception("UserService.GetUserInfoFromOMS ", ex);
            }
        }
    }


}
